<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Services;

use App\Enums\AdminActionTypeEnum;
use App\Enums\AdminCheckEnum;
use App\Enums\CheckStatusEnum;
use App\Events\CheckSuccess;
use App\Exceptions\ApiException;
use App\Format\CheckSuccessFormat;
use App\Format\SearchNoticeFormat;
use App\Models\SearchNotice;

class SearchNoticeService
{
    public function create(SearchNoticeFormat $searchNoticeFormat)
    {
        $searchNoticeFormat->setPushTime(time());
        $searchNoticeFormat->setCheckStatus(CheckStatusEnum::CHECK_ING);
        $searchNoticeFormat->setStatus(1); // 寻找中
        SearchNotice::query()->create($searchNoticeFormat->toArrayNotNull());
    }

    public function adminCreate(SearchNoticeFormat $searchNoticeFormat)
    {
        $searchNoticeFormat->setPushTime(time());
        $searchNoticeFormat->setCheckStatus(CheckStatusEnum::CHECK_ADOPT);
        $searchNoticeFormat->setStatus(1); // 寻找中
        $searchNoticeFormat->setUserId(0);
        $searchNoticeFormat->setExpireTime(time() + (3600 * 24 * 30));
        SearchNotice::query()->create($searchNoticeFormat->toArrayNotNull());
        adminLog(new SearchNotice(), AdminActionTypeEnum::CREATE, '创建寻人启事');
    }

    public function getAdminList(int $page, int $pageSize, array $search)
    {
        $base = SearchNotice::query();

        if (isset($search['status'])) {
            $base->where('status', $search['status']);
        }

        if (isset($search['title'])) {
            $base->where('title', 'like', '%' . $search['title'] . '%');
        }

        if (isset($search['type'])) {
            $base->where('type', $search['type']);
        }

        if (isset($search['start_date'], $search['end_date'])) {
            $base->where('push_time', '>=', strtotime($search['start_date']));
            if (strlen($search['end_date']) > 10) {
                $base->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $base->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }
        if (isset($search['start_date']) && ! isset($search['end_date'])) {
            $base->where('push_time', '>=', strtotime($search['start_date']));
        }
        if (! isset($search['start_date']) && isset($search['end_date'])) {
            if (strlen($search['end_date']) > 10) {
                $base->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $base->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }
        if (isset($search['check_status'])) {
            $base->where('check_status', $search['check_status']);
        }

        $count = $base->count();

        $list = $base->skip(($page - 1) * $pageSize)->take($pageSize)
            ->orderByDesc('id')
            ->get()->toArray();

        if (! empty($search)) {
            adminLog(new SearchNotice(), AdminActionTypeEnum::SELECT, '查询启事');
        }
        return [
            'count' => $count,
            'list' => $list,
            'page' => $page,
            'page_size' => $pageSize,
        ];
    }

    public function getLists(int $page, int $pageSize, array $search)
    {
        $base = SearchNotice::query()
            ->where('expire_time', '>', time())
            ->where('status', 1)->where('check_status', CheckStatusEnum::CHECK_ADOPT);

        if (isset($search['type'])) {
            $base->where('type', $search['type']);
        }

        if (isset($search['start_date'], $search['end_date'])) {
            $base->where('push_time', '>=', strtotime($search['start_date']));
            if (strlen($search['end_date']) > 10) {
                $base->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $base->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }
        if (isset($search['start_date']) && ! isset($search['end_date'])) {
            $base->where('push_time', '>=', strtotime($search['start_date']));
        }
        if (! isset($search['start_date']) && isset($search['end_date'])) {
            if (strlen($search['end_date']) > 10) {
                $base->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $base->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }
        if (isset($search['title'])) {
            $base->where('title', 'like', '%' . $search['title'] . '%');
        }

        $count = $base->count();

        $list = $base->skip(($page - 1) * $pageSize)->take($pageSize)
            ->orderByDesc('id')
            ->get()->toArray();

        return [
            'count' => $count,
            'list' => $list,
            'page' => $page,
            'page_size' => $pageSize,
        ];
    }

    public function getOwnLists(int $page, int $pageSize, int $userId, array $search)
    {
        $base = SearchNotice::query()->where('status', 1)->where('user_id', $userId);

        if (isset($search['type'])) {
            $base->where('type', $search['type']);
        }

        if (isset($search['check_status'])) {
            $base->where('check_status', $search['check_status']);
        }

        if (isset($search['start_date'], $search['end_date'])) {
            $base->where('push_time', '>=', strtotime($search['start_date']));
            if (strlen($search['end_date']) > 10) {
                $base->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $base->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }
        if (isset($search['start_date']) && ! isset($search['end_date'])) {
            $base->where('push_time', '>=', strtotime($search['start_date']));
        }
        if (! isset($search['start_date']) && isset($search['end_date'])) {
            if (strlen($search['end_date']) > 10) {
                $base->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $base->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }

        $count = $base->count();

        $list = $base->skip(($page - 1) * $pageSize)->take($pageSize)
            ->orderByDesc('id')
            ->get()->toArray();

        return [
            'count' => $count,
            'list' => $list,
            'page' => $page,
            'page_size' => $pageSize,
        ];
    }

    public function getDetail(int $id)
    {
        $res = SearchNotice::query()->find($id);
        if (empty($res)) {
            throw new ApiException('无此启事', 1);
        }
        return $res->toArray();
    }

    public function check(int $id, int $checkStatus, string $checkMessage)
    {
        /** @var null|SearchNotice $notice */
        $notice = SearchNotice::query()->find($id);
        if (empty($notice)) {
            throw new ApiException('启事未找到', 1);
        }
        $notice->check_status = $checkStatus;
        $notice->check_message = $checkMessage;
        $notice->expire_time = time() + (3600 * 24 * 30);
        $notice->save();
        adminLog($notice, AdminActionTypeEnum::UPDATE, '审核启事');
        event(new CheckSuccess(new CheckSuccessFormat([
            'check_status' => $checkStatus,
            'check_type' => AdminCheckEnum::CHECK_SEARCH_NOTICE,
            'user_id' => $notice->user_id,
        ])));
        return true;
    }

    public function delete(int $id)
    {
        /** @var null|SearchNotice $searchNotice */
        $searchNotice = SearchNotice::query()->find($id);
        if (empty($searchNotice)) {
            throw new ApiException('启事不存在', 1);
        }
        adminLog($searchNotice, AdminActionTypeEnum::DELETE, '删除启事');
        $searchNotice->delete();
        return true;
    }
}
